
coos.canvas = function(){};
coos.canvas.bind = function(){
	if(coos.browser.msie){
		coos.include.script("excanvas.js");
	}
};
coos.canvas.init = function(){
	var divs = coos.$tag("div");
	var len = divs.length;
	for(var i = 0; i < len; i++)
	{
		if(divs[i].getAttribute("canvasTitle") != null){
			coos.canvas.title(divs[i]);
		}
	}
};
coos.canvas.getContext = function(el){
	var canvas = coos.$$(el);
	if(canvas.getContext){
		return canvas.getContext('2d');
	}else{
		return null;
	}
};
coos.canvas.title = function(obj){
	var attr = obj.getAttribute("canvasTitle").toJson();
	var color = attr.color || "red";
	var radius = attr.radius || 8;
	var width = attr.width || parseInt(obj.style.width,10) || 150;
	var height = attr.height || parseInt(obj.style.height,10) || 30;
	var corner = attr.corner || 6;
	var style = attr.style || "";
	var backgroundColor = attr.backgroundColor || "";
	var alpha = attr.alpha || 1;
	var canvasid = attr.id || (obj.id + "_canvas");
	var canvas = coos.$create("canvas");
	canvas.id = canvasid;
	canvas.width = width;
	canvas.height = height;
	var top = coos.$top(obj);
	var left =  coos.$left(obj);
	if(obj.style.display == "none"){
		top = coos.$top(obj.parentNode);
		left =  coos.$left(obj.parentNode);
	}
	canvas.style.cssText = "position: absolute;top:" + top + "px;left:" + left + "px;" + style;
	coos.$append(document.body,canvas);
	if(coos.browser.msie){
		G_vmlCanvasManager.initElement(canvas);
	}
	var ctx = canvas.getContext('2d');
	ctx.beginPath();
	ctx.strokeStyle = color;
	var tx = 0.5;
	height = height-corner;
	ctx.moveTo(tx,tx+radius);
	ctx.arc(radius,radius+tx,radius,-Math.PI,-Math.PI/2,false);
	ctx.lineTo(width-radius,tx);
	ctx.arc(width-radius-tx,radius+tx,radius,-Math.PI/2,0,false);
	ctx.lineTo(width,height-radius);
	ctx.arc(width-radius-tx,height+tx-radius,radius,0,Math.PI/2,false);
	ctx.lineTo(width/2+corner/2,height+tx);
	ctx.lineTo(width/2,height+corner);
	ctx.lineTo(width/2-corner/2,height+tx);
	ctx.lineTo(tx+radius,height+tx);
	ctx.arc(tx+radius,height+tx-radius,radius,Math.PI/2,Math.PI,false);
	ctx.lineTo(tx,tx+radius);
	ctx.globalAlpha = alpha;
	if(backgroundColor != ""){
		ctx.fillStyle = backgroundColor;
		ctx.fill();
	}else{
		ctx.stroke();
	}
};
coos.canvas.title.show = function(obj){
	var obj = coos.$$(obj);
	coos.$show(obj);
	var attr = obj.getAttribute("canvasTitle").toJson();
	var canvasid = attr.id || (obj.id + "_canvas");
	var cv = coos.$(canvasid);
	cv.style.top = coos.$top(obj) + "px";
	cv.style.left = coos.$left(obj) + "px";
	coos.$show(cv);
};
coos.canvas.title.hide = function(obj){
	var obj = coos.$$(obj);
	var attr = obj.getAttribute("canvasTitle").toJson();
	var canvasid = attr.id || (obj.id + "_canvas");
	coos.$hide(obj);
	coos.$hide(canvasid);
};
coos.canvas.roundedRect = function(ctx,x,y,width,height,radius,color){
	ctx.beginPath();
	if(!color){
		color = ctx.fillStyle;
	}
	ctx.strokeStyle = color;
	ctx.moveTo(x,y+radius);
	ctx.lineTo(x,y+height-radius);
	ctx.quadraticCurveTo(x,y+height,x+radius,y+height);
	ctx.lineTo(x+width-radius,y+height);
	ctx.quadraticCurveTo(x+width,y+height,x+width,y+height-radius);
	ctx.lineTo(x+width,y+radius);
	ctx.quadraticCurveTo(x+width,y,x+width-radius,y);
	ctx.lineTo(x+radius,y);
	ctx.quadraticCurveTo(x,y,x,y+radius);
	ctx.stroke();
};
coos.canvas.bind();
coos.onloadEvent(coos.canvas.init);
